using AndyPike.Snowflake.Core.Exceptions;
using AndyPike.Snowflake.Core.Messages;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;

namespace AndyPike.Snowflake.Core.Tests.Unit.Messages
{
    [TestFixture]
    public class When_validating_an_account_registration
    {
        [Test]
        public void Should_not_throw_an_exception_if_all_the_values_are_valid()
        {
            var registrationMessage = new RegistrationMessage { UserName = "andypike", Password = "123456", Email = "someone@somewhere.com" };
            registrationMessage.Validate();
        }

        [Test]
        [ExpectedException(typeof(InvalidUserNameException))]
        public void Should_throw_an_exception_if_user_name_is_null()
        {
            var registrationMessage = new RegistrationMessage { UserName = null, Password = "123456", Email = "someone@somewhere.com" };
            registrationMessage.Validate();
        }

        [Test]
        [ExpectedException(typeof(InvalidUserNameException))]
        public void Should_throw_an_exception_if_user_name_is_less_than_4_characters()
        {
            var registrationMessage = new RegistrationMessage { UserName = "123", Password = "123456", Email = "someone@somewhere.com" };
            registrationMessage.Validate();
        }

        [Test]
        [ExpectedException(typeof(InvalidPasswordException))]
        public void Should_throw_an_exception_if_password_is_null()
        {
            var registrationMessage = new RegistrationMessage { UserName = "andypike", Password = null, Email = "someone@somewhere.com" };
            registrationMessage.Validate();
        }

        [Test]
        [ExpectedException(typeof(InvalidPasswordException))]
        public void Should_throw_an_exception_if_password_is_less_than_6_characters()
        {
            var registrationMessage = new RegistrationMessage { UserName = "andypike", Password = "12345", Email = "someone@somewhere.com" };
            registrationMessage.Validate();
        }

        [Test]
        [ExpectedException(typeof(InvalidEmailException))]
        public void Should_throw_an_exception_if_email_is_null()
        {
            var registrationMessage = new RegistrationMessage { UserName = "andypike", Password = "123456", Email = null };
            registrationMessage.Validate();
        }

        [Test]
        [ExpectedException(typeof(InvalidEmailException))]
        public void Should_throw_an_exception_if_email_is_not_in_a_valid_format()
        {
            var registrationMessage = new RegistrationMessage { UserName = "andypike", Password = "123456", Email = "blah" };
            registrationMessage.Validate();
        }
    }

    [TestFixture]
    public class When_retrieving_the_password_from_the_registration_message
    {
        [Test]
        public void Should_return_the_MD5_hash_for_a_valid_password()
        {
            var registrationMessage = new RegistrationMessage { UserName = "andypike", Password = "andypike", Email = "someone@somewhere.com" };

            Assert.That(registrationMessage.Password, Is.EqualTo("8958D00667F8878E3A06BF80F10B2D43"));
        }
    }
}